package common.fengkai.filepost.JVM;


import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author Fengkai
 * @creat 2021/12/16
 */
public class TestClaaLoader {

       public static void main(String[] args) throws ClassNotFoundException, UnsupportedEncodingException {
           Map<String , FileDefine> fileDefineMap = new ConcurrentHashMap<>();
           String path = TestClaaLoader.class.getResource("").getPath();
           String decode = URLDecoder.decode(path, "utf-8");
           File file = new File(decode);
           File[] files = file.listFiles();
           for (File watchFile : files) {
               FileDefine fileDefine = new FileDefine();
               fileDefine.setLastDefine(watchFile.lastModified());
               fileDefineMap.put(watchFile.getPath(), fileDefine);
           }

           //定时任务·
           //定时任务
           ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
           scheduledExecutorService.scheduleAtFixedRate(new WatchDog(fileDefineMap), 3, 3, TimeUnit.SECONDS);

           MyClassLoader w = new MyClassLoader();
           while (true) {
               Class clazz = w.findClass("MyLog");
               try {
                   ILog myLog = (ILog) clazz.newInstance();
                   myLog.log();
                   Thread.sleep(1000);
               }catch (Exception e) {
                   e.printStackTrace();
               }
           }


       }
}
